package cn.fboost.mtos.controller;

import cn.fboost.mtos.annotation.IgnoreTokenCheck;
import cn.fboost.mtos.annotation.RoleIs;
import cn.fboost.mtos.dto.Response;
import cn.fboost.mtos.dto.internal.Session;
import cn.fboost.mtos.dto.request.CategoryAddRequest;
import cn.fboost.mtos.dto.request.CategoryDeleteRequest;
import cn.fboost.mtos.dto.request.CategoryUpdateRequest;
import cn.fboost.mtos.service.CategoryService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 商品分类控制器
 */
@RestController
@RequestMapping("category")
public class CategoryController {
    private final CategoryService categoryService;

    public CategoryController(CategoryService categoryService) {
        this.categoryService = categoryService;
    }

    @IgnoreTokenCheck
    @GetMapping("list")
    public Response list() {
        return categoryService.list();
    }

    /**
     * 添加商品分类 - 管理员接口
     *
     * @param request {@link CategoryAddRequest}
     * @return {@link Response}
     */
    @RoleIs({Session.Role.ADMIN})
    @PostMapping("add")
    public Response add(@RequestBody @Validated CategoryAddRequest request) {
        return categoryService.add(request);
    }

    /**
     * 删除商品分类 - 管理员接口
     *
     * @param request {@link CategoryDeleteRequest}
     * @return {@link Response}
     */
    @RoleIs({Session.Role.ADMIN})
    @PostMapping("delete")
    public Response delete(@RequestBody @Validated CategoryDeleteRequest request) {
        return categoryService.delete(request);
    }

    /**
     * 修改商品分类 - 管理员接口
     *
     * @param request {@link CategoryUpdateRequest}
     * @return {@link Response}
     */
    @RoleIs({Session.Role.ADMIN})
    @PostMapping("update")
    public Response update(@RequestBody @Validated CategoryUpdateRequest request) {
        return categoryService.update(request);
    }
}
